package com.kgc.scd.controller;

import com.kgc.scd.bean.HotKey;
import com.kgc.scd.bean.SeckillNotifyResult;
import com.kgc.scd.contant.HotKeyConstant;
import com.kgc.scd.contant.SecKillMQConstant;
import com.kgc.scd.producer.AccountPayResultProducer;
import com.kgc.scd.producer.HotKeyProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.jms.Destination;
import java.util.List;
import java.util.Map;

/**
* @author: HarryXzy
* @date: 2022/6/15 11:45
* @return:
* @description: 秒杀消息中间件
*/
@Slf4j
@RestController
public class SecKillMQController {

    @Autowired(required = false)
    private JmsMessagingTemplate jmsMessagingTemplate;

    @Autowired
    private AccountPayResultProducer accountPayResultProducer;

    @PostMapping("/secKillNotifyUser")
    public String secKillNotifyUser(@RequestBody List<SeckillNotifyResult> getNowNeedNotify){
        //创建一个目标消息队列对象（消息的缓存容器，缓存发送的消息体）
        Destination destination = new ActiveMQQueue(SecKillMQConstant.NOTIFY_USER_SECKILL_P2P);
        //发送消息到目标队列
        jmsMessagingTemplate.convertAndSend(destination,getNowNeedNotify);
        return "success";
    }

    /**
     * @author : 98730
     * @date   : 2022/6/18
     * @param  : [java.lang.String, java.util.Map<java.lang.String,java.lang.String>]
     * @return : java.lang.String
     * @description : 生成订单、支付回调消息队列
     */
    @PostMapping("/payOrderResultNotifyMQ")
    public String payOrderResultNotifyMQ(@RequestParam String mQName, @RequestParam Map<String,String> resultMap) {
        log.info("开始运行，生成订单、支付回调消息队列");
        accountPayResultProducer.payOrderResultNotify(mQName, resultMap);
        return "success";
    }
}
